package com.ltkj.ticket.service.impl;

import com.ltkj.common.core.exception.ServiceException;
import com.ltkj.common.core.utils.DateUtils;
import com.ltkj.common.core.utils.StringUtils;
import com.ltkj.common.datascope.annotation.DataScope;
import com.ltkj.common.security.utils.SecurityUtils;
import com.ltkj.ticket.domain.TicVenues;
import com.ltkj.ticket.mapper.TicVenuesMapper;
import com.ltkj.ticket.service.ITicVenuesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 场馆门票Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-08-30
 */
@Service
public class TicVenuesServiceImpl implements ITicVenuesService 
{
    private static final Logger log = LoggerFactory.getLogger(TicVenuesServiceImpl.class);
    @Autowired
    private TicVenuesMapper ticVenuesMapper;

    /**
     * 查询场馆门票
     * 
     * @param venuesId 场馆门票主键
     * @return 场馆门票
     */
    @Override
    public TicVenues selectTicVenuesByVenuesId(Long venuesId)
    {
        return ticVenuesMapper.selectTicVenuesByVenuesId(venuesId);
    }

    /**
     * 查询场馆门票列表
     * 
     * @param ticVenues 场馆门票
     * @return 场馆门票
     */
    @Override
    @DataScope(storeAlias = "g")
    public List<TicVenues> selectTicVenuesList(TicVenues ticVenues)
    {
        ticVenues.setIsDel("0");
        return ticVenuesMapper.selectTicVenuesList(ticVenues);
    }

    /**
     * 新增场馆门票
     * 
     * @param ticVenues 场馆门票
     * @return 结果
     */
    @Override
    public int insertTicVenues(TicVenues ticVenues)
    {
        ticVenues.setCreateTime(DateUtils.getNowDate());
        ticVenues.setIsDel("0");
        String username = SecurityUtils.getUsername();
        ticVenues.setCreateBy(username);
        ticVenues.setVenuesStatus("2");
        return ticVenuesMapper.insertTicVenues(ticVenues);
    }

    /**
     * 修改场馆门票
     * 
     * @param ticVenues 场馆门票
     * @return 结果
     */
    @Override
    public int updateTicVenues(TicVenues ticVenues)
    {
        ticVenues.setUpdateTime(DateUtils.getNowDate());
        ticVenues.setIsDel("0");
        String username = SecurityUtils.getUsername();
        ticVenues.setUpdateBy(username);
        return ticVenuesMapper.updateTicVenues(ticVenues);
    }

    /**
     * 批量删除场馆门票
     * 
     * @param venuesIds 需要删除的场馆门票主键
     * @return 结果
     */
    @Override
    public int deleteTicVenuesByVenuesIds(Long[] venuesIds)
    {
        int num=0;
        String username = SecurityUtils.getUsername();
        if(null!=venuesIds&&venuesIds.length>0){
            for(int i=0;i<venuesIds.length;i++){
                //查询信息
                TicVenues ticVenues = ticVenuesMapper.selectTicVenuesByVenuesId(venuesIds[i]);
                if( StringUtils.isNotNull(ticVenues)){
                    ticVenues.setUpdateTime(DateUtils.getNowDate());
                    ticVenues.setUpdateBy(username);
                    ticVenues.setIsDel("1");
                    ticVenuesMapper.updateTicVenues(ticVenues);
                    num++;
                }
            }
            return num;

        }else {
            log.info("批量删除门票信息未查询到信息");
            throw new ServiceException("批量删除门票信息未查询到信息 ");
        }

    }

    /**
     * 删除场馆门票信息
     * 
     * @param venuesId 场馆门票主键
     * @return 结果
     */
    @Override
    public int deleteTicVenuesByVenuesId(Long venuesId)
    {
        TicVenues ticVenues = ticVenuesMapper.selectTicVenuesByVenuesId(venuesId);
        ticVenues.setUpdateTime(DateUtils.getNowDate());
        ticVenues.setIsDel("1");
        String username = SecurityUtils.getUsername();
        ticVenues.setUpdateBy(username);
        return ticVenuesMapper.updateTicVenues(ticVenues);

    }

    @Override
    public int offshelf(Long venuesId) {
        TicVenues ticVenues = ticVenuesMapper.selectTicVenuesByVenuesId(venuesId);
        if ("1".equals(ticVenues.getIsDel())) {
            throw new ServiceException("数据不存在");
        }
        ticVenues.setVenuesStatus("1");
        ticVenues.setUpdateTime(DateUtils.getNowDate());
        String username = SecurityUtils.getUsername();
        ticVenues.setUpdateBy(username);
        return ticVenuesMapper.updateTicVenues(ticVenues);
    }

    @Override
    public int onshelf(Long venuesId) {
        TicVenues ticVenues = ticVenuesMapper.selectTicVenuesByVenuesId(venuesId);
        if ("1".equals(ticVenues.getIsDel())) {
            throw new ServiceException("数据不存在");
        }
        ticVenues.setVenuesStatus("2");
        ticVenues.setUpdateTime(DateUtils.getNowDate());
        String username = SecurityUtils.getUsername();
        ticVenues.setUpdateBy(username);
        return ticVenuesMapper.updateTicVenues(ticVenues);
    }
}
